package com.lyh.nacosconfig9001.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author liyanhao
 * @Date 2023-02-24 12:01
 * RefreshScope 刷新会打断定时任务
 */
@RefreshScope
@Configuration
public class SchedulConfig implements ApplicationListener<RefreshScopeRefreshedEvent> {
    @Value(value = "${test}")
    private String test;

    @Scheduled(cron = "*/3 * * * * ?")
    public void test() {
        DateTimeFormatter STANDARD_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(STANDARD_FORMATTER.format(LocalDateTime.now()) + " : the test value is " + test);
    }

    //解决RefreshScope 打断定时任务
    @Override
    public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
        this.test();
    }
}
